﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Threading;


namespace HGZP.PLL
{
    /// <summary>
    /// 作用: 利用WebClient 下载文件(Http-->本地)
    /// 作者: 郑文亮
    /// 日期: 2007-4-26 11:22
    /// 
    /// </summary>
    static public class WebClientDownload
    {
        //网络连接(代理)
        static public bool bFlagUseProxy = false;   //是否使用代理
        static public string ProxyServer = string.Empty;//代理服务器地址
        static public string ProxyServerPort = "80";//代理服务器端口号
        //
        static public bool DownloadFile(string Url, string localPathAndName)
        {
            try
            {
                if (bFlagUseProxy == true) //使用代理的情况
                {
                    if (CheckValid.IsValidInteger(ProxyServerPort) == false)
                    {
                        Public.globalErrorString = " 代理服务器的端口号不正确　不是整数 ";
                        return false;
                    }
                    WebProxy myProxy = new WebProxy(ProxyServer, Convert.ToInt16(ProxyServerPort));
                    WebRequest.DefaultWebProxy = myProxy;

                }
                else  //不使用代理的情况
                {
                    //WebRequest.DefaultWebProxy = null;
                }
                FileInfo myFileInfo = new FileInfo(localPathAndName);
                if (File.Exists(localPathAndName))
                {
                    myFileInfo.IsReadOnly = false;
                }
                
                WebClient myWebClient = new WebClient();
                myWebClient.DownloadFile(Url, localPathAndName);
            }
            catch (Exception ee)
            {

                Public.globalErrorString = ee.Message.ToString() + "\r\n可能是 " + Url + "找不到";//\r\n或者　代理服务器设置不正确
                
                return false;
            }
            return true;
        }
        static public bool UploadFile(string Url, string localPathAndName)
        {
            try
            {
                if (bFlagUseProxy == true) //使用代理的情况
                {
                    if (CheckValid.IsValidInteger(ProxyServerPort) == false)
                    {
                        Public.globalErrorString = " 代理服务器的端口号不正确　不是整数 ";
                        return false;
                    }
                    WebProxy myProxy = new WebProxy(ProxyServer, Convert.ToInt16(ProxyServerPort));
                    WebRequest.DefaultWebProxy = myProxy;

                }
                else  //不使用代理的情况
                {
                    //WebRequest.DefaultWebProxy = null;
                }

                WebClient myWebClient = new WebClient();
                myWebClient.UploadFile(Url, localPathAndName);
              
               
            }
            catch (Exception ee)
            {
                Public.globalErrorString = ee.Message.ToString() ;
                return false;
            }
            return true;
        }
    }
}
